<?php // khoi dong gio hang
	if(!isset($_SESSION['GioHang']))
	{
		$_SESSION['GioHang'] = array();
	}
	$GioHang = $_SESSION['GioHang'];
?>
<?php // xu ly gio hang
	if(isset($_REQUEST['btnAddToCart']))
    {
        $MaHH = $_REQUEST['btnAddToCart'];
        $MatHang = $GioHang[$MaHH];
		if(isset($MatHang))
		{
			$MatHang['SoLuong'] += 1;
		}
		else
		{
			$sql = "SELECT * FROM HangHoa WHERE MaHH=$MaHH";
			$rows = mysql_query($sql);
			$hh = mysql_fetch_array($rows);
			
			$MatHang = array(MaHH=>$hh['MaHH'], TenHH=>$hh['TenHH'], DonGia=>$hh['DonGia'], GiamGia=>$hh['GiamGia'], SoLuong=>1);
		}
		$GioHang[$MaHH] = $MatHang;
    }
    else if(isset($_REQUEST['btnUpdateCart']))
    {
    	foreach($GioHang as $MaHH=>$MatHang)
    	{
    		$MatHang['SoLuong'] = $_REQUEST['qty_'.$MaHH];
    		$GioHang[$MaHH] = $MatHang;
    	}
    }
    else if(isset($_REQUEST['btnClearCart']))
    {
    	$GioHang = array();
    }
    else if(isset($_REQUEST['btnRemoveFromCart']))
    {
    	$MaHH = $_REQUEST['btnRemoveFromCart'];
    	unset($GioHang[$MaHH]);
    }
    $_SESSION['GioHang'] = $GioHang;
?>
